package calendar;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.extension.calendar.CalendarWrapperLocal;
import org.multimedia.jolitorask.extension.calendar.CalendarWrapperLocalHome;
import org.multimedia.jolitorask.extension.calendar.util.timer.CalendarTimerLocal;
import org.multimedia.jolitorask.extension.calendar.util.timer.CalendarTimerLocalHome;

public class ParseCalendars extends HttpServlet implements ServletContextListener {

	private static final long serialVersionUID = 1L;

	private static final String JNDI_NAME_WRAPPER = "calendarWrapperSessionLocal";
	private static final String JNDI_NAME_TIMER = "calendarTimerSessionLocal";
	
	public ParseCalendars() {
		super();
	}   	
	
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

	    res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();
	    
	    Context initContext = null;
	    Hashtable env = new Hashtable(1);
	    Object beanObject = null;
	    
	    	
	    try {
	    		
			initContext = new javax.naming.InitialContext(env);
	    	beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
	    	
	    	CalendarWrapperLocal calendar = ((CalendarWrapperLocalHome)beanObject).create();
	    	
	    	calendar.parseAll();
	    	
	    }catch (Exception e) {
	    	out.println(false);
	    	return;
		}

		out.println(true);
		
	    
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	public void contextInitialized(ServletContextEvent sce) {
    
	    	
		   try {
		    	
		    	InitialContext initContext = new javax.naming.InitialContext(new Hashtable(1));
		    	Object beanObject = initContext.lookup(JNDI_NAME_TIMER);

		    	CalendarTimerLocal c = ((CalendarTimerLocalHome)beanObject).create();
		    	c.startTimer();
		    	
		    	
		    }catch (Exception e) {
		    	e.printStackTrace();
		    	return;
			}

			
		    	
		
	}
	
}
